import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:lenovo
 * Date:2024-06-02
 * Time:21:23
 */
class MyNaturalNumbers {
        private String value;
        private int length;

        public MyNaturalNumbers() {
            value = "";
            length = 0;
        }
        public MyNaturalNumbers(String num) {
            setValue(num);
        }
        public void setValue(String num) {
            // Remove comma separators if present
            num = num.replaceAll(",", "");
            // Check if num contains only digits
            if (num.matches("\\d+")) {
                value = num;
                length = value.length();
            } else {
                System.out.println("Invalid input! Please enter a valid natural number.");
            }
        }
        public void displayValue() {
            System.out.println("Value: " + value);
        }
        public MyInteger toMyInteger() {
            return new MyInteger(value);
        }
        public MyNaturalNumbers add(MyNaturalNumbers num) {
            String result = "";
            int carry = 0;
            int maxLength = Math.max(length, num.length);
            for (int i = 0; i < maxLength; i++) {
                int digit1 = (i < length) ? Character.getNumericValue(value.charAt(length - 1 - i)) : 0;
                int digit2 = (i < num.length) ? Character.getNumericValue(num.value.charAt(num.length - 1 - i)) : 0;
                int sum = digit1 + digit2 + carry;
                carry = sum / 10;
                result = (sum % 10) + result;
            }
            if (carry > 0) {
                result = carry + result;
            }
            return new MyNaturalNumbers(result);
        }
        public MyNaturalNumbers subtract(MyNaturalNumbers num) {
            if (this.compareTo(num) < 0) {
                System.out.println("Cannot subtract a larger number from a smaller number.");
                return new MyNaturalNumbers();
            }
            String result = "";
            int borrow = 0;
            int maxLength = Math.max(length, num.length);
            for (int i = 0; i < maxLength; i++) {
                int digit1 = (i < length) ? Character.getNumericValue(value.charAt(length - 1 - i)) : 0;
                int digit2 = (i < num.length) ? Character.getNumericValue(num.value.charAt(num.length - 1 - i)) : 0;

                digit1 -= borrow;

                if (digit1 < digit2) {
                    borrow = 1;
                    digit1 += 10;
                } else {
                    borrow = 0;
                }

                int diff = digit1 - digit2;
                result = diff + result;
            }

            // Remove leading zeros
            result = result.replaceFirst("^0+(?!$)", "");

            return new MyNaturalNumbers(result);
        }

        public int compareTo(MyNaturalNumbers num) {
            if (length > num.length) {
                return 1;
            } else if (length < num.length) {
                return -1;
            } else {
                return value.compareTo(num.value);
            }
        }

        @Override
        public String toString() {
            return value;
        }
    }

    class MyInteger extends MyNaturalNumbers {
        public MyInteger() {
            super();
        }

        public MyInteger(String num) {
            super(num);
        }

        public MyInteger(MyNaturalNumbers num) {
            super(num.toString());
        }
    }

    public class Test {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);

            System.out.print("Enter a natural number: ");
            String input1 = scanner.nextLine();
            MyNaturalNumbers num1 = new MyNaturalNumbers(input1);

            System.out.print("Enter another natural number: ");
            String input2 = scanner.nextLine();
            MyNaturalNumbers num2 = new MyNaturalNumbers(input2);

            MyNaturalNumbers sum = num1.add(num2);
            MyNaturalNumbers diff = num1.subtract(num2);

            System.out.println("Sum: " + sum);
            System.out.println("Difference: " + diff);

            MyInteger int1 = num1.toMyInteger();
            MyInteger int2 = new MyInteger(num2);

            System.out.println("Integer 1: " + int1);
            System.out.println("Integer 2: " + int2);

            scanner.close();
        }
    }

